<?php
class FileManager extends MY_Controller
{
	const DIR_IMG = 'assets/uploads/images/';
	const DIR_THB = 'assets/uploads/thumb/';

	public function __construct()
	{
		parent::__construct();

		$this->load->helper(array('directory', 'download', 'path'));
		$this->load->library('image_lib');
		$this->load->library('upload');

		$this->data['styles'] = array(
			"{$this->data['base_url']}assets/css/text.css",
			"{$this->data['base_url']}assets/css/black/ui.css",
			"{$this->data['base_url']}assets/backend/css/filemanager.css",
		);

		$this->data['scripts'] = array(
			"http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js",
			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js",
			"{$this->data["base_url"]}assets/backend/js/jstree/jquery.tree.min.js",
			"{$this->data["base_url"]}assets/backend/js/ajaxupload.js",
			"{$this->data["base_url"]}assets/backend/js/filemanager.js",
		);
	}

	public function index($field = 'image')
	{
		$this->data['field'] = $field;
		$this->load->view('backend/filemanager', $this->data);
	}

	public function files()
	{
		clearstatcache();

		$directory = ($this->input->post('directory')) ? self::DIR_IMG . $this->input->post('directory') : self::DIR_IMG;
		$file_data = directory_map($directory, 1);

		$files = array();
		if($file_data)
		{
			foreach ($file_data as $key => $file)
			{
				if (@is_file($directory . $file))
				{
					$files[$key]['file'] = $file;
					$files[$key]['filename'] = $file;
					$files[$key]['size'] = file_size($directory . $file);
					$files[$key]['thumb'] = base_path(). $this->resize($file);
				}
			}
		}

		echo json_encode($files);
	}

	public function directory()
	{
		clearstatcache();

		$directory = ($this->input->post('directory')) ? self::DIR_IMG . $this->input->post('directory') : self::DIR_IMG;
		$file_data = directory_map($directory, 1);
		$files = array();
		$files['xx'] = print_r($file_data, 1);
		if($file_data)
		{
			$key = 0;
			foreach ($file_data as $file)
			{
				if (@is_dir($directory . $file))
				{
					$files[$key]['data'] = $file;
					$files[$key]['attributes']['directory'] = substr($directory . $file, strlen(self::DIR_IMG));

					$children = directory_map($directory . '/' . $files[$key]['attributes']['directory'], 1);

					if ($children) {
						$files[$key]['children'] = ' ';
					}

					$key++;
				}
			}
		}

		echo json_encode($files);
	}

	public function image()
	{
		$response = array();
		$image = $this->input->post('image');
		if (file_exists(self::DIR_IMG . $image))
		{
			$response['_100x100'] = $this->resize($image);
			$response['_40x40'] = $this->resize($image, 40, 40);
		}

		echo json_encode($response);
	}

	public function create()
	{
		$json = array();
		if (isset($this->input->post['directory']))
		{
			if (isset($this->input->post['name']) || $this->input->post['name'])
			{
				$directory = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->input->post['directory']), '/');

				if (!is_dir($directory)) {
					$json['error'] = $this->language->get('error_directory');
				}

				if (file_exists($directory . '/' . str_replace('../', '', $this->input->post['name']))) {
					$json['error'] = 'Đã có thư mục ';
				}
			} else {
				$json['error'] = $this->language->get('error_name');
			}
		} else {
			$json['error'] = $this->language->get('error_directory');
		}

		if (!$this->user->hasPermission('modify', 'common/filemanager')) {
			$json['error'] = $this->language->get('error_permission');
		}

		if (!isset($json['error'])) {
			mkdir($directory . '/' . str_replace('../', '', $this->input->post['name']), 0777);
			$json['success'] = 'OK';
		}

		echo json_encode($json);
	}

	public function delete()
	{

	}

	public function rename()
	{

	}

	public function move()
	{

	}

	public function copy()
	{

	}

	public function upload()
	{
		$upload_info['error'] = TRUE;
		$upload_info['message'] = 'Access Denied';
		$upload_info['image'] = '';

		$upload_config['upload_path'] = './assets/uploads/images/';
		$upload_config['allowed_types'] = 'gif|jpg|png';
		//$upload_config['encrypt_name'] = TRUE;

		$this->upload->initialize($upload_config);
		if ($this->upload->do_upload('image'))
		{
			$upload_info['message'] = '';
			$upload_info['error'] = FALSE;
			$upload_info['image'] = $this->upload->data();
			$upload_info['image']['thumb'] = $this->resize($upload_info['image']['file_name']);
		}
		else
		{			
			$upload_info['message'] = $this->upload->display_errors("", "\n");
		}
		echo json_encode($upload_info);
	}

	public function resize($image, $width = 100, $height = 100)
	{
		$this->load->library('imagelib');
		$this->imagelib->initialize(self::DIR_IMG. $image);
		$new_image = basename($image).".{$width}x{$height}.".file_extension($image);

		$this->imagelib->resize($width, $height);
		$this->imagelib->save(self::DIR_THB . $new_image);

		return self::DIR_THB . $new_image;
	}
}